
"""
    pycoopiler - a compiler developed to PCS2056 course (Compilers) at Poli-USP
    Copyright (C) 2008 Andre Felipe Santos    
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

import unittest

from pycoopiler.token import token
from pycoopiler.tokenizer import tokenizer
import logging

class testtokenizer(unittest.TestCase):
    
    tkzr = None
    
    def setUp(self):
        pass
        
    def test_next_token_1(self):
        self.tkzr = tokenizer(open('cases/test_tokenizer_1', 'r'))
        tokenlist = "PLUS", "MINUS", "TIMES", "DIV", "MOD", "NOT", "OR", "AND", "OPEN_RB", \
                    "CLOSE_RB", "OPEN_SB", "CLOSE_SB", "OPEN_CB", "CLOSE_CB", "SEMICOLON", \
                    "COMMA", "ATTR", "GT", "LT", "GTEQ", "LTEQ", "COMP", "DIFF", "EOF"
        print '\n'
        for t in tokenlist :
            logging.debug("Lido token %s", self.tkzr.current)
            self.assertEqual(self.tkzr.current.type, t)
            self.tkzr.next_token()
        
    def test_next_token_2(self):
        pass
        
    def test_next_token_3(self):
        pass
    
    def test_next_token_4(self):
        pass

    def test_next_token_5(self):
        
        self.tkzr = tokenizer(open('cases/test_tokenizer_5', 'r'))
        print '\n'        
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'int')
        self.assertEqual(self.tkzr.current.type, 'INT')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'main')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'void')
        self.assertEqual(self.tkzr.current.type, 'VOID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '{')
        self.assertEqual(self.tkzr.current.type, 'OPEN_CB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'printf')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '42')
        self.assertEqual(self.tkzr.current.type, 'CONST')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ';')
        self.assertEqual(self.tkzr.current.type, 'SEMICOLON')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'int')
        self.assertEqual(self.tkzr.current.type, 'INT')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '[')
        self.assertEqual(self.tkzr.current.type, 'OPEN_SB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '5')
        self.assertEqual(self.tkzr.current.type, 'CONST')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ']')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_SB')        
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'vetor')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ';')
        self.assertEqual(self.tkzr.current.type, 'SEMICOLON')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'if')
        self.assertEqual(self.tkzr.current.type, 'IF')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '!')
        self.assertEqual(self.tkzr.current.type, 'NOT')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'a')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '==')
        self.assertEqual(self.tkzr.current.type, 'COMP')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'b')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'return')
        self.assertEqual(self.tkzr.current.type, 'RETURN')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '0')
        self.assertEqual(self.tkzr.current.type, 'CONST')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ';')
        self.assertEqual(self.tkzr.current.type, 'SEMICOLON')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '}')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_CB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'int')
        self.assertEqual(self.tkzr.current.type, 'INT')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'func')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'void')
        self.assertEqual(self.tkzr.current.type, 'VOID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '{')
        self.assertEqual(self.tkzr.current.type, 'OPEN_CB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'printf')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '42')
        self.assertEqual(self.tkzr.current.type, 'CONST')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ';')
        self.assertEqual(self.tkzr.current.type, 'SEMICOLON')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'int')
        self.assertEqual(self.tkzr.current.type, 'INT')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '[')
        self.assertEqual(self.tkzr.current.type, 'OPEN_SB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '5')
        self.assertEqual(self.tkzr.current.type, 'CONST')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ']')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_SB')        
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'vetor')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ';')
        self.assertEqual(self.tkzr.current.type, 'SEMICOLON')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'if')
        self.assertEqual(self.tkzr.current.type, 'IF')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '!')
        self.assertEqual(self.tkzr.current.type, 'NOT')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '(')
        self.assertEqual(self.tkzr.current.type, 'OPEN_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'a')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '==')
        self.assertEqual(self.tkzr.current.type, 'COMP')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'b')
        self.assertEqual(self.tkzr.current.type, 'ID')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ')')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_RB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, 'return')
        self.assertEqual(self.tkzr.current.type, 'RETURN')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '0')
        self.assertEqual(self.tkzr.current.type, 'CONST')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, ';')
        self.assertEqual(self.tkzr.current.type, 'SEMICOLON')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.info, '}')
        self.assertEqual(self.tkzr.current.type, 'CLOSE_CB')
        self.tkzr.next_token() 
        
        logging.debug("Lido token %s", self.tkzr.current) 
        self.assertEqual(self.tkzr.current.type, 'EOF')
        self.tkzr.next_token() 
    
        
    def TestSuite():
        return unittest.makeSuite(testautomaton)
    
    if __name__ == '__main__':
        unittest.main()
 
        